from django.db import models

# 用户
class User(models.Model):
    username = models.CharField(max_length=20, verbose_name="账号")
    password = models.CharField(max_length=255, verbose_name="密码")
    phone = models.CharField(max_length=11, blank=True, null=True, verbose_name="手机号")
    email = models.CharField(max_length=255,verbose_name='邮箱',blank=True,null=True)
    is_binded = models.BooleanField(default=False, verbose_name="是否绑定微信")
    avatar = models.URLField(max_length=255, blank=True, null=True, verbose_name="头像")
    nick_name = models.CharField(max_length=255,verbose_name='昵称',blank=True,null=True)
    qq_openid = models.CharField(max_length=100, unique=True, blank=True, null=True, verbose_name="QQ开放ID")
    wechat_openid = models.CharField(max_length=100, unique=True, blank=True, null=True, verbose_name="微信开放ID")
    def userinfo(self):
        return {
            'username':self.username,
            'phone':self.phone,
            'email':self.email,
            'nick_name':self.nick_name,
            'avatar':self.avatar,
            'is_binded':self.is_binded,
            'qq_openid': self.qq_openid,
            'wechat_openid':self.wechat_openid
        }
    class Mate:
        db_table = 'user'

    
# 分类
class Category(models.Model):
    name = models.CharField(max_length=50, unique=True, verbose_name="分类名称")

    class Mate:
        db_table = 'category'

# 标签
class Tag(models.Model):
    name = models.CharField(max_length=30, unique=True, verbose_name="标签名称")

    def to_tag(self):
        return {
           'name' : self.name
        }

    class Mate:
        db_table = 'tag'

# 文章
class Article(models.Model):
    title = models.CharField(max_length=100, verbose_name="文章标题名字")
    author = models.CharField(max_length=255,verbose_name='作者',blank=True,null=True)
    category = models.ForeignKey(to=Category, on_delete=models.SET_NULL, blank=True, null=True, verbose_name="所属分类")
    publish_time = models.DateTimeField(auto_now_add=True, verbose_name="发表时间") 
    updated_time = models.DateTimeField(auto_now=True,verbose_name='更新时间',blank=True,null=True)
    read_count = models.IntegerField(default=0, verbose_name="阅读量")  
    comment_count = models.IntegerField(default=0, verbose_name="评论量")  
    like_count = models.IntegerField(default=0, verbose_name="点赞量") 
    tag = models.ManyToManyField(to=Tag,  blank=True, verbose_name="关联标签") 
    text = models.TextField(verbose_name='文章内容',blank=True,null=True)
    cover_img = models.URLField(verbose_name='封面照片',blank=True,null=True)
    

    def to_article(self):
        """定义实例化方法"""
        return {
            'title': self.title,
            'author': self.author,
            'category': self.category.name,
            'publish_time': self.publish_time,
            'updated_time': self.updated_time,
            'read_count': self.read_count,
            'comment_count': self.comment_count,
            'like_count': self.like_count,
            'text':self.text,
            'tag': self.tag.name,
            'cover_img': self.cover_img
        }

    class Mate:
        db_table = 'article'

# 评论
class Comment(models.Model):
    article = models.ForeignKey(to=Article, on_delete=models.CASCADE, verbose_name="文章id")
    commentator = models.CharField(max_length=255,verbose_name='评论人')
    text = models.TextField(verbose_name="评论内容",blank=True,null=True)
    publish_time = models.DateTimeField(auto_now_add=True, verbose_name="评论内容发布时间")  
    is_responsed = models.BooleanField(default=False,verbose_name='是否有回复')
    reply_to = models.ForeignKey(to="self",on_delete=models.CASCADE, blank=True, null=True, verbose_name="回复目标")
    
    class Meta:
        db_table ='comment'